package com.zpself.module.io.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;


/**
 * @author By ZengPeng
 * @Description 使用netty创建堆外内存测试
 * @date in  2020/7/21 16:13
 * @Modified By
 */
public class ByteBuffTest {
    private static final byte[] CONTENT = new byte[1024];

    public static void main(String[] args) {
        for (int i = 0; i <CONTENT.length; i++) {
            if(i%2==0){
                CONTENT[i] = 97 +1;
            }
            CONTENT[i] = 97 -1;
        }

        int loop = 3000000;
        System.out.println(new String(CONTENT));
        //1.堆外内存池
        long star = System.currentTimeMillis();
        ByteBuf poolByteBuffer = null;
        for (int i = 0; i < loop; i++) {
            poolByteBuffer =  PooledByteBufAllocator.DEFAULT.directBuffer(1024);
            poolByteBuffer.writeBytes(CONTENT);
            poolByteBuffer.release();
        }
        long end1 = System.currentTimeMillis();
        System.out.println("内存池的非堆内存缓冲区  耗费时间："+(end1-star)+"ms");



        //2.堆外内存
        ByteBuf buffer = null;
        for (int i = 0; i < loop; i++) {
            buffer = Unpooled.directBuffer(1024);
            buffer.writeBytes(CONTENT);
            buffer.release();
        }
        long end2 = System.currentTimeMillis();
        System.out.println("非内存池创建的非堆内存缓冲区  耗费时间："+(end2-end1)+"ms");
    }
}
